/* //////////////// BASE OPTION SET //////////////// */

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var myLatlng = new google.maps.LatLng(51.165691, 10.451526);
            
/* //////////////// INITIALIZE MAP //////////////// */

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    
    var mapOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: myLatlng
    };
    
    var map = new google.maps.Map(document.getElementById('map_canvas_one'), mapOptions);
        
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
         
    var marker = new google.maps.Marker({
        position: myLatlng,
        draggable:true,
        map: map,
        title: 'Truck',
        icon:'http://www.abteilungweb.com/data/bootstrap/img/map_truck.png'
    });
        
    /* //////////////// DEFINE INFO WINDOW STUFF //////////////// */

    infowindow = new google.maps.InfoWindow({  
        size: new google.maps.Size(150, 50)  
    });  
       
    //add eventlistener click to the map  
    google.maps.event.addListener(map, 'click', function() {  
        infowindow.close();  
    });  
        
    /* //////////////// DRAG EVENT //////////////// */
    
    google.maps.event.addListener(marker, 'dragend', function() {
        // Get the Current position, where the pointer was dropped
        var point = marker.getPosition();
        // Center the map at given point
        map.panTo(point);
        // Update the inputfield
        document.getElementById('geoa').value=point.lat();
        document.getElementById('geob').value=point.lng();
    }); 

    /* //////////////// CLICK EVENT //////////////// */

    var onMarkerClick = function() {  
        var marker = this;  
        var latLng = marker.getPosition();  
        infowindow.setContent('<h3>Marker position is:</h3>' + latLng.lat() + ', ' + latLng.lng());  
        infowindow.open(map, marker);  
    };  
        
    google.maps.event.addListener(marker, 'click', onMarkerClick);  
}

// ======================================================================================
// ======================================================================================

/* //////////////// CALC ROUTE //////////////// */
        
function calcRoute() {
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            var loc = results[0].geometry.location;
            alert(loc);
        }
    });
} 
      
      
google.maps.event.addDomListener(window, 'load', initialize);

/* //////////////// SET MAP IN TAB //////////////// */

$(document).ready(function() { 
   	
    // Function added to help reset map and container boundaries
    $("#showMap").click(function() {
        $("#rB").css({'display':'block'});
        $("#map_canvas_one").css({'width':'570px', 'height':'450px'});
        initialize();
    });
        
    initialize(); 

}); /* END DOCUMENT READY FUNCTION*/